//
//  SettingsVC.swift
//  HelloSwift
//
//  Created by alankong on 2016/11/17.
//  Copyright © 2016年 tuotiansudai. All rights reserved.
//

import UIKit

class SettingsVC: UIViewController {

    var button: UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "设置";
        self.view.backgroundColor = UIColor.white
        
        setupViews()
    }
    
    func setupViews() {
        self.button = UIButton();
        self.button?.size = CGSize(width:200, height:50)
        self.button?.center = CGPoint(x: self.view.width/2, y: self.view.height/2)
        self.button?.setTitle("设置页", for: UIControlState.normal)
        self.button?.backgroundColor = UIColor.red
        self.view.addSubview(self.button!)
        self.button?.addTarget(self, action: #selector(clickAction(sender:)), for: UIControlEvents.touchUpInside)
    }
    
    func clickAction(sender: UIButton?) {
        UIView.animate(withDuration: 0.3) {
            let distance: UInt32 = 100
            let flagX: Bool = Bool(arc4random() % 2 > 0);
            let offsetX: CGFloat = flagX ? CGFloat(arc4random() % distance) : CGFloat(arc4random() % distance) * -1
            
            let flagY: Bool = Bool(arc4random() % 2 > 0);
            let offsetY: CGFloat = flagY ? CGFloat(arc4random() % distance) : CGFloat(arc4random() % distance) * -1
            
            self.button?.center = CGPoint(x: self.view.width/2 + offsetX, y: self.view.height/2 + offsetY)
        }
    }

}
